import java.io.File;
import java.io.IOException;
import java.lang.management.ThreadInfo;
import java.sql.DriverManager;
import java.sql.SQLException;


import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;



public class Main {

	/**
	 * @param args
	 */
	static int count =0;
	static Connection  conexion = null;
	static String directory ="";
	
	public static void main(String[] args) {
		initdriver();
		directory ="C:\\Users\\Juan\\Downloads\\Rage Against The Machine";
		File f = new File(directory);
		recursiva(f,0);
		System.out.println(count);
	}
	
	public static void recursiva (File f, int i){
		File []content = f.listFiles();
		for(int j=0; j<content.length; ++j){
			if(content[j].isDirectory()) recursiva(content[j],i);
			else{
				datos(content[j]);
			}
		}
	}
		
	public static void datos(File f){
		++count;
		if(f!=null && ismp3(f)){
			MP3File mp3file = null;
			try {
				//System.out.println(f.getName());
				mp3file = new MP3File(f);
			} 
			catch (IOException | TagException e) {
				// TODO Auto-generated catch block
				System.out.println(f.getName());
				e.printStackTrace();
			}
			String artista="kk";
			String titulo="kk";
			String ruta = MysqlEscape("\\media"+f.getAbsolutePath().substring(directory.length()));
			if(mp3file.hasID3v1Tag()){
				artista = MysqlEscape(mp3file.getID3v1Tag().getArtist());
				titulo = MysqlEscape(mp3file.getID3v1Tag().getTitle());
			}
			else if(mp3file.hasID3v2Tag()){
				artista = MysqlEscape(mp3file.getID3v2Tag().getAuthorComposer());
				titulo = MysqlEscape(mp3file.getID3v2Tag().getSongTitle());				
			}
			
			if(artista.equals("") || titulo.equals("") || artista.equals("kk") || titulo.equals("kk") ){
				String [] datos= datoscutres(f.getName()); 
				artista = datos[0];
				titulo = datos[1];
			}
			String query="INSERT OR UPDATE INTO audio (autor,titulo,ruta) VALUES('"+artista+"','"+titulo+"','"+ruta+"')";

			//System.out.println(query);
			ThreadInsert th = new ThreadInsert(query, conexion);
			th.run();
		}
	}
	
	public static boolean ismp3(File f){
		String s = f.getName();
		if(s.contains(".mp3")){
				return true;
		}
		return false;
	}
	
	public static String[] datoscutres(String s){
		String artista="kkk";
		String titulo="kkk";
		String[] s2 = s.split(".mp3");
		if(s2.length>0){
			String [] s3= s2[0].split("-");
			if(s3.length>1){
				System.out.println("----> "+s3[0]+"  "+s3[1]);
				artista= MysqlEscape(s3[0]);
				titulo= MysqlEscape(s3[1]);
			}
		}
		String [] ret = new String [2];
		ret[0]=artista;
		ret[1]=titulo;
		return ret;
		
	}
	
	public static String MysqlEscape(String s){
		String s1=new String();
		for(int i=0; i<s.length(); ++i){
			if(s.charAt(i)=='\''){
				s1=s1+"\\"+"\'";
			}
			else if(s.charAt(i)=='\\'){
				s1=s1+'\\'+'\\';
			}
			else{
				s1=s1+s.charAt(i);
			}
					
		}
		return s1;
	}
	public static void initdriver(){
		try
		{
		   Class.forName("com.mysql.jdbc.Driver");
		} catch (Exception e)
		{
		   e.printStackTrace();
		}
		initdb();
	}
	
	public  static void initdb(){
		try {
			conexion = (Connection) DriverManager.getConnection ("jdbc:mysql://localhost/audio","root", "");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	


}
